/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.gerenciadorfinanceiro.face;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.gerenciadorfinanceiro.ejb.entity.Outcome;
import org.gerenciadorfinanceiro.ejb.entity.OutcomeSource;
import org.gerenciadorfinanceiro.ejb.entity.User;
import org.gerenciadorfinanceiro.ejb.session.BusinessViolation;
import org.gerenciadorfinanceiro.ejb.session.interfaces.OutcomeSessionLocal;
import org.gerenciadorfinanceiro.ejb.session.interfaces.OutcomeSourceSessionLocal;

/**
 *
 * @author eduardo
 */

public class OutcomeFace {

    @EJB
    private OutcomeSessionLocal outcomeBean;

    @EJB
    private OutcomeSourceSessionLocal sourceBean;

    private Outcome outcomeInEdit = null;
    private List<Outcome> outcomes = null;
    private Map<String, OutcomeSource> sources = null;


    public String doStartAddOutcome() {
        this.setOutcomeInEdit(new Outcome(0));
        return "startAddOutcome";
    }

    public String doFinishAddOutcome() throws BusinessViolation {
        Outcome o = this.getOutcomeInEdit();
        User user = (User)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("usuarioLogado");
        o.setOwner(user);
        o.setData(new Date());
        this.getOutcomeBean().addOutcome(o);
        outcomes = null;
        outcomeInEdit = null;
        return "finishAddOutcome";
    }

    public String doStartUpdateOutcome() {
        return "startUpdateOutcome";
    }

    public String doFinishUpdateOutcome() throws BusinessViolation {
        Outcome o = this.getOutcomeInEdit();
        o.setData(new Date());
        this.getOutcomeBean().updateOutcome(o);
        outcomes = null;
        outcomeInEdit = null;
        return "finishUpdateOutcome";
    }

    public String doRemoveOutcome() throws BusinessViolation {
        this.getOutcomeBean().deleteOutcome(this.getOutcomeInEdit());
        outcomes = null;
        outcomeInEdit = null;
        return "finishRemoveOutcome";
    }

    public OutcomeFace() {
    }

    /**
     * @return the outcomeBean
     */
    public OutcomeSessionLocal getOutcomeBean() {
        return outcomeBean;
    }

    /**
     * @return the outcomeBean
     */
    public OutcomeSourceSessionLocal getSourceBean() {
        return sourceBean;
    }

    /**
     * @return the outcomeInEdit
     */
    public Outcome getOutcomeInEdit() {
        return outcomeInEdit;
    }

    /**
     * @param outcomeInEdit the outcomeInEdit to set
     */
    public void setOutcomeInEdit(Outcome outcomeInEdit) {
        this.outcomeInEdit = outcomeInEdit;
    }

    /**
     * @return the outcomes
     */
    public List<Outcome> getOutcomes() {
        if (this.outcomes == null) {
            User user = (User)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("usuarioLogado");
            Calendar c = GregorianCalendar.getInstance();
            outcomes = outcomeBean.getAllOutcomes(user, c.get(Calendar.MONTH) + 1, c.get(Calendar.YEAR));
        }

        return outcomes;
    }

    /**
     * @param outcomes the outcomes to set
     */
    public void setOutcomes(List<Outcome> outcomes) {
        this.outcomes = outcomes;
    }

    /**
     * @return the sources
     */
    public Map<String, OutcomeSource> getSources() {
        if (this.sources == null) {
            Map<String, OutcomeSource> items = new TreeMap<String, OutcomeSource>();
            List<OutcomeSource> ls = sourceBean.getAllSources();

            for (OutcomeSource outcomeSource : ls) {
                items.put(outcomeSource.getDescriptionSource(), outcomeSource);
            }

            this.setSources(items);
        }

        return sources;
    }

    /**
     * @param sources the sources to set
     */
    public void setSources(Map<String, OutcomeSource> sources) {
        this.sources = sources;
    }

}
